﻿<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="DatabaseSchema"
    targetNamespace="http://tempuri.org/DatabaseSchema.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/DatabaseSchema.xsd"
    xmlns:mstns="http://tempuri.org/DatabaseSchema.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:annotation id="copyrights">
    <xs:documentation xml:lang="en">
      Database schema for project AppML.
      Copyright 2013 steven.n.yang@gmail.com. All rights reserved.
    </xs:documentation>
  </xs:annotation>

  <xs:annotation id="todo">
    <xs:documentation xml:lang="en">
      1. [Data Type] allow any string in data type.
      2. [Column] length is required for specific data types: char, nchar, varchar, nvarchar.
      3. [Column] prisicion and scale are required for specific data types: decimal and numeric.
      4. [Column] identitySeed and identityIncrement are only used for specific data types
      
    </xs:documentation>
  </xs:annotation>
  
  <xs:element name="databaseSchema" type ="DatabaseSchemaType"/>

  <xs:complexType name="DatabaseSchemaType">
    <xs:sequence>
      <xs:element name="tables" type="TablesType" minOccurs="0" maxOccurs="1"/>
    </xs:sequence>
    <xs:attribute name="schema" type="xs:string"/>
    <xs:attribute name="version" type="xs:string"/>
  </xs:complexType>

  <xs:complexType name="TablesType">
    <xs:sequence>
      <xs:element name="table" type="TableType" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="TableType">
    <xs:sequence>
      <xs:element name="columns" type="ColumnsType" minOccurs="1" maxOccurs="1"/>
      <xs:element name="primaryKey" type="IndexType" minOccurs="0" maxOccurs="1"/>
      <xs:element name="foreignKeys" type="ForeignKeysType" minOccurs="0" maxOccurs="1"/>
      <xs:element name="indexes" type="IndexesType" minOccurs="0" maxOccurs="1"/>
      <xs:element name="description" type="xs:string" minOccurs="0" maxOccurs="1"/>
    </xs:sequence>
    <xs:attribute name="name" type="xs:string" use="required"/>
  </xs:complexType>

  <xs:complexType name="ColumnsType">
    <xs:sequence>
      <xs:element name="column" type="ColumnType" minOccurs="1" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="ColumnType">
    <xs:sequence>
      <xs:element name="description" type="xs:string" minOccurs="0" maxOccurs="1"/>
    </xs:sequence>
    <xs:attribute name="name" type="xs:string" use="required"/>
    <xs:attribute name="datatype" type="DataTypeType" use="required"/>
    <xs:attribute name="allowNull" type="xs:boolean"/>
    <xs:attribute name="length" type="xs:unsignedInt"/>
    <xs:attribute name="precision" type="xs:unsignedInt"/>
    <xs:attribute name="scale" type="xs:unsignedInt"/>
    <xs:attribute name="identitySeed" type="xs:unsignedInt"/>
    <xs:attribute name="identityIncrement" type="xs:unsignedInt"/>
  </xs:complexType>

  <xs:simpleType name="DataTypeType">
    <xs:restriction base="xs:string">
      <xs:enumeration value="bigint"/>
      <xs:enumeration value="bit"/>
      <xs:enumeration value="char"/>
      <xs:enumeration value="date"/>
      <xs:enumeration value="datetime"/>
      <xs:enumeration value="datetime2"/>
      <xs:enumeration value="datetimeoffset7"/>
      <xs:enumeration value="decimal"/>
      <xs:enumeration value="float"/>
      <xs:enumeration value="int"/>
      <xs:enumeration value="money"/>
      <xs:enumeration value="nchar"/>
      <xs:enumeration value="numeric"/>
      <xs:enumeration value="nvarchar"/>
      <xs:enumeration value="nvarchar(MAX)"/>
      <xs:enumeration value="real"/>
      <xs:enumeration value="smalldatetime"/>
      <xs:enumeration value="smalint"/>
      <xs:enumeration value="smallmoney"/>
      <xs:enumeration value="time7"/>
      <xs:enumeration value="timestamp"/>
      <xs:enumeration value="tinyint"/>
      <xs:enumeration value="uniqueidentifier"/>
      <xs:enumeration value="varchar"/>
      <xs:enumeration value="varchar(MAX)"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:complexType name="IndexesType">
    <xs:sequence>
      <xs:element name="index" type="IndexType" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="IndexType">
    <xs:sequence>
      <xs:element name="column" minOccurs="1" maxOccurs="unbounded">
        <xs:complexType>
          <xs:attribute name="columnName" type="xs:string" use="required"/>
          <xs:attribute name="order" type="xs:unsignedInt"/>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
    <xs:attribute name="name"/>
  </xs:complexType>

  <xs:complexType name="ForeignKeysType">
    <xs:sequence>
      <xs:element name="foreignKey" type="ForeignKeyType" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="ForeignKeyType">
    <xs:sequence>
      <xs:element name="column" minOccurs="1" maxOccurs="unbounded">
        <xs:complexType>
          <xs:attribute name="columnName" type="xs:string" use="required"/>
          <xs:attribute name="foreignColumnName" type="xs:string" use="required"/>
          <xs:attribute name="order" type="xs:unsignedInt"/>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
    <xs:attribute name="name"/>
    <xs:attribute name="foreignTableName" use="required"/>
  </xs:complexType>
</xs:schema>
